package com.y5neko.RadiantKnightExploit.misc;

import com.gn.lab.GNButton;
import com.y5neko.RadiantKnightExploit.exp.VulTypes;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class Tools {
    public static final String BLACK = "\033[30m";
    public static final String RED = "\033[31m";
    public static final String GREEN = "\033[32m";
    public static final String YELLOW = "\033[33m";
    public static final String BLUE = "\033[34m";
    public static final String PURPLE = "\033[35m";
    public static final String CYAN = "\033[36m";
    public static final String WHITE = "\033[37m";
    public static final String RESET = "\033[0m";

    private static final Map<String, String> COLOR_MAP = new HashMap<>();

    static {
        COLOR_MAP.put("BLACK", BLACK);
        COLOR_MAP.put("RED", RED);
        COLOR_MAP.put("GREEN", GREEN);
        COLOR_MAP.put("YELLOW", YELLOW);
        COLOR_MAP.put("BLUE", BLUE);
        COLOR_MAP.put("PURPLE", PURPLE);
        COLOR_MAP.put("CYAN", CYAN);
        COLOR_MAP.put("WHITE", WHITE);
    }

    public static Button getImgButton(String imgPath) {
        Button button = new Button();
        Image imageClose = new Image(imgPath);
        ImageView imageViewClose = new ImageView(imageClose);
        imageViewClose.setFitHeight(20);
        imageViewClose.setPreserveRatio(true);
        button.setGraphic(imageViewClose);
        button.setBackground(new Background(new BackgroundFill(Color.TRANSPARENT, CornerRadii.EMPTY, Insets.EMPTY)));
        button.setContentDisplay(ContentDisplay.GRAPHIC_ONLY); // 仅显示图形内容

        return button;
    }

    public static GNButton getGNButton(String string) {
        GNButton button = new GNButton();
        button.setText(string);
        button.setStyle("-gn-transition-color : #00000000; -gn-transition-text : black;");
        button.setMinHeight(35);
        button.setFont(Font.font("Microsoft YaHei", 14));
        return button;
    }

    public static ScrollPane getScrollPane(Node node, boolean scrollBarPolicy) {
        ScrollPane scrollPane = new ScrollPane();
        scrollPane.setContent(node);
        if (scrollBarPolicy){
            scrollPane.setVbarPolicy(ScrollPane.ScrollBarPolicy.ALWAYS);
        }
        scrollPane.setFitToWidth(true);
        scrollPane.getStylesheets().add("css/ScrollPane.css");
        scrollPane.getStyleClass().add("edge-to-edge");
        return scrollPane;
    }

    /**
     * 获取一个含有指定资产漏洞的VBox
     * @param vulTypes 资产列表
     * @return VBox
     */
    public static VBox getVulTypeBox(String[] vulTypes) {
        VBox vulTypeBox = new VBox();
        for (String vulType : vulTypes) {
            GNButton buttonVul = Tools.getGNButton(vulType);
            buttonVul.setOnMouseClicked(event -> {
                System.out.println(vulType);
            });
            vulTypeBox.getChildren().add(buttonVul);
        }
        return vulTypeBox;
    }

    public static VBox getVulTypeBox(String[] vulTypes, GridPane gridPane, String assetType, HBox removeNededHBox) {
        VBox vulTypeBox = new VBox();
        for (String vulType : vulTypes) {
            GNButton buttonVul = Tools.getGNButton(VulTypes.getVulTypeString(vulType));
            buttonVul.setOnMouseClicked(event -> {
                System.out.println(vulType);
                System.out.println("清除了最右侧VBox");
                gridPane.getChildren().remove(gridPane.getChildren().get(1));

                try {
                    Class<?> clazz = Class.forName("com.y5neko.RadiantKnightExploit.exp." + assetType + "." + vulType);
                    Method method = clazz.getMethod("getHBox");
                    HBox hBox = (HBox) method.invoke(null);
                    gridPane.add(hBox, 1, 0, 1, 1);
                } catch (ClassNotFoundException | InvocationTargetException | NoSuchMethodException |
                         IllegalAccessException e) {
                    throw new RuntimeException(e);
                }
            });
            vulTypeBox.getChildren().add(buttonVul);
        }
        return vulTypeBox;
    }

    /**
     * 改变字体颜色
     * @param str 字符串
     * @param color 颜色
     * @return 包含ANSI转义的字符串
     */
    public static String color(String str, String color){
        String colorCode = COLOR_MAP.getOrDefault(color, RESET);
        return colorCode + str + RESET;
    }
}
